﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ObjectManager;

namespace QuanLyDiem
{
    public partial class frmLop_ThayDoi : Form
    {
        private static frmLop_ThayDoi frm = null;
        private Lop lop = new Lop();

        private frmLop_ThayDoi()
        {
            InitializeComponent();
            List<string> temp = (new BoMon()).ListName();
            temp.Insert(0, "Chọn");
            cmbBoMon.DataSource = temp;
            txtMa.Enabled = false;
        }

        public static frmLop_ThayDoi Create()
        {
            return Create(false);
        }

        public static frmLop_ThayDoi Create(bool child)
        {
            if (frm == null)
            {
                frm = new frmLop_ThayDoi();
            }
            if (child)
            {
                frm.ControlBox = false;
                frm.MinimizeBox = false;
                frm.MaximizeBox = false;
                frm.WindowState = FormWindowState.Maximized;
            }
            frm.RefreshData();
            return frm;
        }

        private void frmLop_ThayDoi_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (frm != null)
            {
                frm.Dispose();
                frm = null;
            }
        }

        private void RefreshData()
        {
            dgvLop.DataSource = lop.ListAll();
        }

        private void cmniChiTiet_Click(object sender, EventArgs e)
        {
            btnSua.PerformClick();
        }

        private void cmniXoa_Click(object sender, EventArgs e)
        {
            btnXoa.PerformClick();
        }

        private void dgvLop_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnSua.PerformClick();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvLop.SelectedRows.Count > 0)
            {
                DialogResult dalrs = MessageBox.Show("Bạn có chắc chán muốn xóa " + dgvLop.SelectedRows.Count.ToString() + " lớp không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dalrs == DialogResult.OK)
                {
                    int count = 0;
                    int length = dgvLop.SelectedRows.Count;
                    foreach (DataGridViewRow item in dgvLop.SelectedRows)
                    {
                        Lop lop = new Lop();
                        lop.ThongTin.Ten = item.Cells["cTen"].Value.ToString();
                        int check = lop.Remove();
                        if (check > 0)
                        {
                            count++;
                            dgvLop.Rows.Remove(item);
                        }
                    }
                    if (count > 0)
                    {
                        MessageBox.Show("Đã xóa thành công " + count.ToString() + " lớp", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                MessageBox.Show("Bạn phải chọn các lớp muốn xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dgvLop.SelectedRows.Count > 0)
            {
                Lop lop = new Lop();
                lop.ThongTin.Ma = int.Parse(dgvLop.SelectedRows[0].Cells["cMa"].Value.ToString());
                lop.ThongTin.Ten = dgvLop.SelectedRows[0].Cells["cTen"].Value.ToString();
                lop.ThongTin.BoMon = dgvLop.SelectedRows[0].Cells["cBoMon"].Value.ToString();

                frmLop_Sua frm = frmLop_Sua.Create(lop);
                frm.Owner = this;
                frm.UpdateLopSuccess += new UpdateLop(frm_UpdateLopSuccess);
                frm.ShowDialog();
            }
        }

        private void frm_UpdateLopSuccess(object sender, InfoUpdateLop e)
        {
            dgvLop.SelectedRows[0].Cells["cTen"].Value = e.Lop.ThongTin.Ten;
            dgvLop.SelectedRows[0].Cells["cBoMon"].Value = e.Lop.ThongTin.BoMon;
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (rdMa.Checked)
            {
                dgvLop.DataSource = this.lop.ListAll(txtMa.Text);
            }
            else
            {
                if (cmbBoMon.SelectedIndex == 0)
                {
                    dgvLop.DataSource = this.lop.ListAll(txtTen.Text, "", "");
                }
                else
                {
                    dgvLop.DataSource = this.lop.ListAll(txtTen.Text, "", cmbBoMon.Text);
                }
            }
        }

        private void rdNoMa_CheckedChanged(object sender, EventArgs e)
        {
            if (rdMa.Checked)
            {
                pnTimKiem.Enabled = false;
                txtMa.Enabled = true;
            }
            else
            {
                pnTimKiem.Enabled = true;
                txtMa.Enabled = false;
            }
        }

        private void rdNoMa_CheckedChanged_1(object sender, EventArgs e)
        {

        }
    }
}
